#include "stdafx.h"
#include "compote_mgr.h"
#include "i_UIcompote.h"
#include "i_UIcompote_maker.h"
#include "iltdrawprim.h"
#include "..\clientinterfaces.h"
#include "iltmessage.h"

compote_mgr::compote_mgr():
m_window_w(1024),
m_window_h(768)
{
	g_pLTCDrawPrim->SetTransformType(DRAWPRIM_TRANSFORM_SCREEN);
	g_pLTCDrawPrim->SetColorOp(DRAWPRIM_MODULATE);
	g_pLTCDrawPrim->SetAlphaBlendMode(DRAWPRIM_BLEND_MOD_SRCALPHA);
	g_pLTCDrawPrim->SetZBufferMode(DRAWPRIM_NOZ);
	g_pLTCDrawPrim->SetAlphaTestMode(DRAWPRIM_NOALPHATEST);
	g_pLTCDrawPrim->SetClipMode(DRAWPRIM_FASTCLIP);
	g_pLTCDrawPrim->SetFillMode(DRAWPRIM_FILL);
	g_pLTCDrawPrim->SetCullMode(DRAWPRIM_CULL_NONE);


}

compote_mgr::~compote_mgr()
{
	for (CompoteCollIt it=m_compotos.begin();it!=m_compotos.end();++it)
	{
		if ((*it).second)
		{
			delete (*it).second;
			(*it).second=0;
		}

	}
	m_compotos.clear();

}

compote_mgr& compote_mgr::single()
{
	static compote_mgr _single;
	return _single;

}

void compote_mgr::render()
{
	for (CompoteCollIt it=m_compotos.begin();it!=m_compotos.end();++it)
	{
		if ((*it).second->active())
		{
			(*it).second->render();
		}
		
	}


}

void compote_mgr::update()
{
	for (CompoteCollIt it=m_compotos.begin();it!=m_compotos.end();++it)
	{
		if ((*it).second->active())
		{
			(*it).second->update();
		}

	}


}

bool compote_mgr::add_compote(std::string _name, i_UIcompote* _compote )
{
	CompoteCollIt it=m_compotos.find(_name);
	if (it==m_compotos.end())
	{
		if (_compote)
		{
			_compote->set_windos_size(m_window_w,m_window_h);
			m_compotos.insert(std::make_pair(_name,_compote));
			return true;
		}
			
	}
	return false;
	

}
bool compote_mgr::remove_compote( std::string _name )
{
	CompoteCollIt it=m_compotos.find(_name);
	if (it!=m_compotos.end())
	{
		if ((*it).second)
		{
			delete (*it).second;
			(*it).second=0;
		}
		m_compotos.erase(it);
		return true;
		
	}
	return false;


}
  
void compote_mgr::on_message(ILTMessage_Read * _read_msg)
{
	_read_msg->IncRef();
	_read_msg->SeekTo(0);
	std::string recUI;
	char temp_name[256];
	_read_msg->ReadString(temp_name,256);
	recUI=temp_name;
	if (recUI=="All")
	{
		for (CompoteCollIt it=m_compotos.begin();it!=m_compotos.end();++it)
		{
			
			(*it).second->on_message(_read_msg);
			

		}

	}
	else
	{
		m_compotos[recUI]->on_message(_read_msg);
	}
	_read_msg->DecRef();



	

}

void compote_mgr::build_compotes( i_UIcompote_maker* _maker )
{
	if (!_maker)
	{
		return;
	}
	_maker->make();

	for (;_maker->get_current_compote();_maker->add_index())
	{
		add_compote(_maker->get_current_name(),_maker->get_current_compote());
	}


}

compote_mgr* compote_mgr::get_ptr()
{
	return this;

}